﻿#include <QVBoxLayout>
#include "formpublisher.h"
#include "ui_formpublisher.h"
#include "qstringutils.h"

FormPublisher::FormPublisher(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormPublisher)
{
    ui->setupUi(this);
}

FormPublisher::~FormPublisher()
{
    delete ui;
}

void FormPublisher::setComm(BasePublisher *pub)
{
    m_pub=pub;
    QWidget *w=pub->getConfigWidget();
    setCfgWidget(w);
//    loadUiCfg();
}

void FormPublisher::setCfgWidget(QWidget *w)
{
    QVBoxLayout *vbox = new QVBoxLayout();
    vbox->addWidget(w);
    ui->groupBox->setLayout(vbox);
}

void FormPublisher::on_btnSend_clicked()
{
    sendMsg();
}

void FormPublisher::sendMsg()
{
    QByteArray arr=ui->chbHex->isChecked()?QByteArray::fromHex(ui->txtSend->text().toUtf8())
                                         :ui->txtSend->text().toUtf8();
    m_pub->publish(arr);
    if(ui->chbRecord->isChecked())
    {
        qDebug()<<"发送:"<<ui->txtSend->text();
    }

    QString formattedData = QStringUtils::getFormatString(arr,ui->chbHex->isChecked());
    // 使用HTML格式设置颜色
    QString html = QString("<span style='color:red;'>发送: </span><span>%1</span>").arg(formattedData);
    ui->reciveData->append(html);  // 使用 append 追加内容
}
